﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Cook
{
    class Program
    {
        //语义分析
        private static ISemAnalysis.ISemAnalysis _sem;
        //食谱标准名称
        private static IIngredient.IIngredientData _ingData=new Ingredient.AccessIngredientData();

        //TODO 库文件与菜谱文件的差异

        static void Main(string[] args)
        {
            if (args.Length == 1 && args[0].ToLower() == "-i")//列出所有食材名称
            {
                foreach(string data in _ingData.GetAllIngredientsNames())
                {
                    Console.WriteLine(data);
                }
            }
            else if (args.Length == 2 && (args.Contains("-i") || args.Contains("-I")))//列出最相近的食材名称
            {
                string name = args[0].ToLower() == "-i" ? args[1].ToLower() : args[0].ToLower();
                foreach(string data in _ingData.GetSimilarIngredientsNames(name))
                {
                    Console.WriteLine(data);
                }
            }
            else if (args.Length == 2 && (args.Contains("-s") || args.Contains("-S")))
            {
                string path = args[0].ToLower() == "-s" ? args[1].ToLower() : args[0].ToLower();
                if (FileExists(path, path.Substring(0, path.Replace('\\', '/').LastIndexOf('/') + 1)))
                {
                    OutputFile(path, path.Substring(0, path.Replace('\\', '/').LastIndexOf('/') + 1), false);
                }
            }
            else if (args.Length == 3)
            {
                string path = "";
                if (args[0].ToLower() == "-l" && args[1].ToLower() == "-s")
                {
                    path = args[2].ToLower();
                }
                else if (args[0].ToLower() == "-s" && args[2].ToLower() == "-l")
                {
                    path = args[1].ToLower();
                }
                else
                {
                    Console.WriteLine("Error in Shell Command Arguments!");
                }
                if (FileExists(path, path.Substring(0, path.Replace('\\', '/').LastIndexOf('/') + 1)))
                {
                    OutputFile(path, path.Substring(0, path.Replace('\\', '/').LastIndexOf('/') + 1), true);
                }
            }
            else if (args.Length == 4)
            {
                string inPath = "", outPath = "";
                if (args[0].ToLower() == "-s" && args[2].ToLower() == "-d")
                {
                    inPath = args[1].ToLower();
                    outPath = args[3].ToLower();
                }
                else if (args[0].ToLower() == "-d" && args[2].ToLower() == "-s")
                {
                    inPath = args[3].ToLower();
                    outPath = args[1].ToLower();
                }
                else
                {
                    Console.WriteLine("Error in Shell Command Arguments!");
                }
                if (FileExists(inPath, outPath))
                {
                    OutputFile(inPath, outPath, false);
                }
            }
            else if (args.Length == 5)
            {
                string inPath = "", outPath = "";
                if (args[0].ToLower() == "-l" && args[1].ToLower() == "-s" && args[3].ToLower() == "-d")
                {
                    inPath = args[2].ToLower();
                    outPath = args[4].ToLower();
                }
                else if (args[0].ToLower() == "-l" && args[3].ToLower() == "-s" && args[1].ToLower() == "-d")
                {
                    inPath = args[4].ToLower();
                    outPath = args[2].ToLower();
                }
                else if (args[2].ToLower() == "-l" && args[0].ToLower() == "-s" && args[3].ToLower() == "-d")
                {
                    inPath = args[1].ToLower();
                    outPath = args[4].ToLower();
                }
                else if (args[2].ToLower() == "-l" && args[3].ToLower() == "-s" && args[0].ToLower() == "-d")
                {
                    inPath = args[4].ToLower();
                    outPath = args[1].ToLower();
                }
                else if (args[4].ToLower() == "-l" && args[0].ToLower() == "-s" && args[2].ToLower() == "-d")
                {
                    inPath = args[1].ToLower();
                    outPath = args[3].ToLower();
                }
                else if (args[4].ToLower() == "-l" && args[2].ToLower() == "-s" && args[0].ToLower() == "-d")
                {
                    inPath = args[3].ToLower();
                    outPath = args[1].ToLower();
                }
                else
                {
                    Console.WriteLine("Error in Shell Command Arguments!");
                }
                if (FileExists(inPath, outPath))
                {
                    OutputFile(inPath, outPath, true);
                }
            }
            else
            {
                Console.WriteLine("Error in Shell Command Arguments!");
            }
        }

        //判断文件是否存在
        private static bool FileExists(string inPath,string outPath)
        {
            bool res = false;
            if(File.Exists(inPath))
            {
                if(Directory.Exists(outPath))
                {
                    res = true;
                }
                else
                {
                    Console.WriteLine("Destination File Path doesn't exist!");
                }
            }
            else
            {
                Console.WriteLine("Source File Path doesn't exist!");
            }
            return res;
        }

        //读取文件内容
        private static string GetFileContant(string path)
        {
            if (File.Exists(path))
            {
                String str = "";
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                //开始读入
                str = sr.ReadToEnd();
                //关闭流
                sr.Close();
                fs.Close();

                str = str.Replace("\r\n", "\n").ToUpper();
                return str;
            }
            Console.WriteLine("File doesn't exist!");
            return null;
        }

        //生成中间代码
        private static void OutputFile(string inPath,string outPath, bool isForLib)
        {
            string code = GetFileContant(inPath);
            if (code != null)
            {
                string inP = inPath.Replace('\\', '/');
                _sem = new SemAnalysis.SemAnalysis(code, isForLib, inPath.Substring(inP.LastIndexOf('/') + 1));
                if (_sem.Success())
                {
                    //TODO  对生产库文件还是食谱文件的判断
                    _sem.GetRecipe(outPath);
                }
                else
                {
                    Console.WriteLine("Error in source code!");
                }
            }
        }
    }
}
